package com.learn.spring_mvc.controller;

import com.learn.spring_mvc.bean.Account;
import com.learn.spring_mvc.dto.req.TransferMoneyReq;
import com.learn.spring_mvc.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 请求映射路径配置注解（可用于类或方法上，若配置在类上则类内的所有请求路径都需加上该前缀）
 * · @RequestMapping：接受任何请求，可通过method指定请求类型
 * · @GetMapping：接受Get请求
 * · @PostMapping：接受Post请求
 * . 其他请求语义化注解...
 * <p>
 * 请求参数配置注解
 * · @RequestParam：
 * ·    value：指定Get请求参数名称（表单提交或/xxx?id=1），若未指定则默认为形参名称。也可通过List接收同名参数、Map一次性接收参数
 * ·    required：必须提交该参数，若未接收到则报错
 * ·    defaultValue：默认值，未提交该参数且存在默认值时required不报错
 * · @PathVariable：指定接受路径变量/xxx/{id}
 * · @RequestBody：指定接受Post请求体对象
 * · @RequestHeader：指定请求头信息
 * · @CookieValue：指定Cookie请求头信息
 * 响应体注解
 * · @ResponseBody：springmvc默认返回视图名称，加上该注解后以响应体方式响应数据，可用于类上
 * · @RestController：相当于@Controller+@ResponseBody
 */
@Controller
@RequestMapping("/account")
// @RestController
public class AccountController {
    @Autowired
    private AccountService accountService;
    
    // http://ip:port/account/request/transferMoney?fromAccount=2&toAccount=1&amount=1000
    // http://ip:port/account/requestGet/transferMoney?fromAccount=2&toAccount=1&amount=1000
    @RequestMapping(
            value = {"/request/transferMoney", "/requestGet/transferMoney"},
            method = RequestMethod.GET
    )
    public String transferMoneyRequest(@RequestParam("fromAccount") Integer from, Integer toAccount, Integer amount) {
        accountService.transferMoney(from, toAccount, amount);
        return "/index.jsp";
    }
    
    // http://ip:port/account/get/transferMoney?fromAccount=2&toAccount=1&amount=1000
    @GetMapping("/get/transferMoney")
    public String transferMoney(Integer fromAccount, Integer toAccount, Integer amount) {
        accountService.transferMoney(fromAccount, toAccount, amount);
        return "/index.jsp";
    }
    
    // http://ip:port/account/post/transferMoney（请求体配置参数）
    @PostMapping("/post/transferMoney")
    public String transferMoney(@RequestBody TransferMoneyReq req) {
        accountService.transferMoney(req.getFromAccount(), req.getToAccount(), req.getAmount());
        return "/index.jsp";
    }
    
    // http://ip:port/account/header/transferMoney
    @GetMapping("/header/transferMoney")
    public String transferMoney(
            @RequestHeader Map<String, String> headers,
            @RequestHeader("cookie") String cookies,
            @CookieValue(name = "JSESSIONID", defaultValue = "12") String jsessionid
    ) {
        headers.forEach((key, value) -> {
            System.out.println(key + ": " + value);
        });
        
        System.out.println(cookies);
        
        System.out.println(jsessionid);
        
        return "/index.jsp";
    }
    
    // http://ip:port/account/response/transferMoney
    @GetMapping("/response/transferMoney")
    @ResponseBody
    public Account transferMoney() {
        return new Account();
    }
    
    // http://ip:port/account/http/transferMoney
    // 参数HttpServletRequest、HttpServletResponse无需传递，springmvc框架会自动传入（这两个对象需要单独导包javax.servlet）
    @GetMapping("/http/transferMoney")
    public String transferMoney(HttpServletRequest request, HttpServletResponse response) {
        System.out.println(request.getRequestURL());
        System.out.println(response.getContentType());
        return "/index.jsp";
    }
}
